﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Board.Models
{
    public class Role
    {
        private t_webpages_Roles _role = null;

        public static string ROLE_ADMIN = "Admin";
        public static string ROLE_SITE_ADMIN = "Site Admin";
        public static string ROLE_UNIT_ADMIN = "Unit Admin";
        public static string ROLE_KARDEX_USER = "Kardex User";
        public static string ROLE_KARDEX_VIEWER = "Kardex Viewer";

        public static Role Admin
        {
            get
            {
                return new Role(ROLE_ADMIN);
            }
        }

        public static Role SiteAdmin
        {
            get
            {
                return new Role(ROLE_SITE_ADMIN);
            }
        }

        public static Role UnitAdmin
        {
            get
            {
                return new Role(ROLE_UNIT_ADMIN);
            }
        }

        public static Role KardexUser
        {
            get
            {
                return new Role(ROLE_KARDEX_USER);
            }
        }

        public static Role KardexViewer
        {
            get{return new Role(ROLE_KARDEX_VIEWER);}
        }

        public static List<string> AdminRoleNames
        {
            get
            {
                return new List<string>() { ROLE_ADMIN, ROLE_SITE_ADMIN, ROLE_UNIT_ADMIN };
            }
        }

        public static HashSet<Role> AdminRoles
        {
            get {
                using (DbEntities db = new DbEntities())
                {
                    return new HashSet<Role>(from r in db.t_webpages_Roles
                                             where AdminRoleNames.Contains(r.RoleName)
                                             select new Role() { WebpagesRoles = r });
                }
            }
        }

        public int RoleId
        {
            get { return _role.RoleId; }
        }

        public string RoleName
        {
            get { return _role.RoleName; }
            set
            {
                if (_role == null)_role = new t_webpages_Roles();
                _role.RoleName = value;
            }
        }

        public t_webpages_Roles WebpagesRoles
        {
            get { return _role; }
            set { _role = value; }
        }

        public Role(t_webpages_Roles role)
        {
            _role = role;
        }



        public Role(){}

        public Role(string roleName)
        {
            if (!string.IsNullOrEmpty(roleName))
            {
                using (DbEntities db = new DbEntities())
                {
                    var roles = from r in db.t_webpages_Roles
                                where r.RoleName.Equals(roleName)
                                select r;
                    if (roles.Any())
                    {
                        _role = roles.FirstOrDefault();
                    }
                }
            }
        }

        public Role(int roleId)
        {
            if (roleId > 0)
            {
                using (DbEntities db = new DbEntities())
                {
                    var roles = from r in db.t_webpages_Roles
                                where r.RoleId == roleId
                                select r;
                    if (roles.Any())
                    {
                        _role = roles.FirstOrDefault();
                    }
                }
            }
        }



        public static HashSet<Role> GetRolesById(List<int> roleIds)
        {
            using (DbEntities db = new DbEntities())
            {
                return
                    new HashSet<Role>(from r in db.t_webpages_Roles
                     where roleIds.Contains(r.RoleId)
                     select new Role() {WebpagesRoles = r});
            }
        } 

        public bool Equals(Role role)
        {
            return _role.RoleId == role.RoleId;
        }

        public void Save()
        {
            if (_role != null)
            {
                if (_role.RoleId > 0)
                {
                    //existing record
                    using (DbEntities db = new DbEntities())
                    {
                        var existingRecords = from r in db.t_webpages_Roles
                                             where r.RoleId == _role.RoleId
                                             select r;
                        if (existingRecords.Any())
                        {
                            var existingRecord = existingRecords.FirstOrDefault();
                            if (WebLog.LogValueChanged(db, "webpages_Roles", "RoleName", existingRecord.RoleName,
                                                   _role.RoleName))
                            {
                                existingRecord.RoleName = _role.RoleName;
                            }
                            db.SaveChanges();
                        }
                        else
                        {
                            //log error
                        }
                    }
                }
                else
                {
                    //new record
                    using (DbEntities db = new DbEntities())
                    {
                        db.t_webpages_Roles.Add(_role);
                        db.SaveChanges();
                    }
                }
            }
        }


    }
}